﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;
using HU8.Helpers.Images.Models;

namespace HU8.Helpers.Files.Images
{
    /// <summary>
    /// 图片集合
    /// 与前端的image_uploader配合使用
    /// 作者：刘勇
    /// 日期： 2012年4月19日
    /// </summary>
    public class ImageCollection
    {
        public List<ImageValuePair> Images { get; set; }
        public ImageValuePair DefaultImage
        {
            get
            {
                return Images.FirstOrDefault();
            }
        }

        /// <summary>
        /// 传入ImageString将其转成对象
        /// </summary>
        /// <param name="imageString">1.jpg|description;2.jpg|description2;</param>
        public ImageCollection(string imageString)
        {
            Images = new List<ImageValuePair>();
            if (!string.IsNullOrEmpty(imageString))
            {
                string[] images = imageString.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                foreach (string i in images)
                {
                    string[] vp = i.Split("|".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                    ImageValuePair p = new ImageValuePair();
                    p.ImagePath = vp[0];
                    p.Description = vp[1];
                    Images.Add(p);
                }
            }
        }
        /// <summary>
        /// 删除不在ImageCollection中的文件
        /// </summary>
        public void RemoveDeletedFiles(string originalPath)
        {
            DirectoryInfo dir = new DirectoryInfo(originalPath);
            try
            {
                List<FileInfo> deletedFiles = new List<FileInfo>();
                foreach (FileInfo f in dir.GetFiles())
                {
                    if (Images.Where(i => i.ImagePath == f.Name).Count() == 0)
                    {
                        deletedFiles.Add(f);
                    }
                }
                //删除应该删除的文件
                foreach (FileInfo f in deletedFiles)
                {
                    f.Delete();
                }
            }
            catch (Exception ex)
            {
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="originalPath"></param>
        /// <param name="propertyListingThumbPath"></param>
        /// <param name="propertyListingImagePath"></param>
        /// <param name="thumbConfig"></param>
        /// <param name="watermarkConfig"></param>
        /// <param name="watermarkQuality"></param>
        /// <param name="p"></param>
        public void CompressionWatermark(string originalPath, string propertyListingThumbPath, string propertyListingImagePath, ThumbnailConfig thumbConfig, WatermarkConfig watermarkConfig, int watermarkQuality, ImageValuePair p)
        {
            //源图路径
            string originalImagePath = string.Format("{0}/{1}", originalPath, p.ImagePath);
            if (System.IO.File.Exists(originalImagePath))
            {
                //缩略图路径
                var imgThumPath = string.Format("{0}/{1}", propertyListingThumbPath, p.ImagePath);
                //水印图路径
                var imgWaterMarkPath = string.Format("{0}/{1}", propertyListingImagePath, p.ImagePath);
                System.IO.File.Copy(originalImagePath, imgThumPath, true);
                //压缩缩略图
                ImageHelper.SaveThumbnail(imgThumPath, thumbConfig);
                //保存水印图
                var source = Image.FromFile(originalImagePath);
                Image photoImage = ImageHelper.MakeWatermark(source, watermarkConfig);
                ImageHelper.SaveJpeg(imgWaterMarkPath, photoImage, watermarkQuality);
            }
        }
    }
}
